package org.jeecg.util;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.JsonObject;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jeecg.modules.publish.util.AccessTokenUtil;
import org.jeecgframework.p3.core.util.HttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WechatUtil {


    public static JSONObject getArticleSummary (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getarticlesummary?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }

    public static JSONObject getArticleTotal (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getarticletotal?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }


    public static JSONObject getUserRead (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getuserread?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }


    public static JSONObject getUserShare (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getusershare?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }


    /**
     * 获取用户相关信息
     * @param clientKey
     * @param clientSecret
     * @param beginDate
     * @param endDate
     * @return
     * @throws IOException
     */
    public static JSONObject getusersummary (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getusersummary?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }


    public static JSONObject getusercumulate (String clientKey,String clientSecret,String beginDate,String endDate) throws IOException {
        String accessToken = AccessTokenUtil.getAccseeToken(clientKey, clientSecret);

        String url = "https://api.weixin.qq.com/datacube/getusercumulate?access_token="+accessToken;
        JsonObject paramJson = new JsonObject();
        paramJson.addProperty("begin_date",beginDate);
        paramJson.addProperty("end_date",endDate);

        String result = HttpUtils.doPostJson(url, paramJson.toString());
        JSONObject jsonObject = JSONObject.parseObject(result);


        return  jsonObject;

    }
}
